package com.baichang.taoshunda.controller;

import com.baichang.framework.controller.IController;
import com.baichang.framework.controller.WebBaseController;
import com.baichang.framework.excelption.ServiceException;
import com.baichang.taoshunda.ControllerConstants;
import com.baichang.taoshunda.entity.BackgroundImage;
import com.baichang.taoshunda.service.BackgroundImageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by Administrator on 2018/3/22 0022.
 */
@Controller
@RequestMapping(ControllerConstants.IMAGE)
public class ImageWController extends WebBaseController {

    @Autowired
    private BackgroundImageService backgroundImageService;

    /**
     * @return
     */
    @RequestMapping("/findImage")
    public String findImage() {
        return "taoshunda/material/image";
    }

    /**
     * @param request
     * @param response
     */
    @RequestMapping("/queryImage")
    public void queryImage(HttpServletRequest request, HttpServletResponse response) {
        init(request, response, new IController() {
            @Override
            public Object initData(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
                return backgroundImageService.queryImage();
            }
        });
    }

    /**
     * @param request
     * @param response
     */
    @RequestMapping("/addImage")
    public void addImage(final HttpServletRequest request, HttpServletResponse response) {
        init(request, response, new IController() {
            @Override
            public Object initData(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
                String picture = getString(request, "picture", "");
                return backgroundImageService.addImage(picture);
            }
        });
    }

    @RequestMapping("/deletePic")
    public void deletePic(final HttpServletRequest request, HttpServletResponse response){
        init(request, response, new IController() {
            @Override
            public Object initData(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
                Integer id = getInt(request,"id",0);
                return backgroundImageService.deletePic(id );
            }
        });
    }
}
